﻿using CommonDevelop;
using CommonEngine;
using CommonFP;
using ManageDto;
using ManageService;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using WebAdmin.Handler;

namespace WebAdmin.Areas.Org.Controllers
{
    /// <summary>
    /// 岗位管理
    /// </summary>
    [Area("Org")]
    [Route("Org/[controller]/[action]")]
    public class PositionController : BaseController
    {
        private readonly IPositionService _PostService;
        /// <summary>
        /// 构造注入
        /// </summary>
        public PositionController(IPositionService PostService)
        {
            this._PostService = PostService;
        }

        /// <summary>
        /// 视图列表
        /// </summary>
        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.Title = "岗位管理";
            return View();
        }

        #region--Select--

        /// <summary>
        /// 列表数据
        /// </summary>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetTreeList(string keyword)
        {
            List<PositionDto> data = this._PostService.GetList<PositionDto>(keyword).ToList();
            return this.SuccessJson(data);
        }

        #endregion

        #region--增删改--

        [HttpPost]
        [AjaxOnly]
        public ActionResult NodeAdd()
        {
            PositionDto en = new PositionDto();
            return this.SuccessJson(en);
        }

        [HttpPost]
        [AjaxOnly]
        public ActionResult NodeDelete(string keyValue)
        {
            Validation<bool> vb = this._PostService.Delete(keyValue);
            var ValTuple = vb.Outcome(a => a);
            if (ValTuple.Errors.IsEmpty())
                return ValTuple.result ? this.Success("操作成功") : this.Fail("操作失败");
            else
                return this.Fail(ValTuple.Errors);
        }

        [HttpPost]
        [AjaxOnly]
        public ActionResult NodeEdit(string keyValue)
        {
            var outcome = this._PostService.Get<PositionDto>(keyValue);
            return outcome.Match(() => this.Fail("Null Error"),
                en => this.SuccessJson(en));
        }

        [HttpPost]
        [AjaxOnly]
        [DisableRequestSizeLimit]
        public ActionResult NodeSave([FromForm]string DataText)
        {
            PositionDto em = PageUtility.FormMap<PositionDto>(DataText);
            if (em == null)
                return this.Fail("表单解读失败");
            if (em.ShortName.IsEmpty())
                return this.Fail("简称不能为空");
            if (em.FullName.IsEmpty())
                return this.Fail("岗位名称不能为空");

            Validation<bool> vb = FP.Error("未执行");
            if (em.ModifyUserId.IsEmpty())
                vb = this._PostService.Add(em);
            else
                vb = this._PostService.Save(em);

            var ValTuple = vb.Outcome(a => a);
            if (ValTuple.Errors.IsEmpty())
                return ValTuple.result ? this.Success("操作成功") : this.Fail("操作失败");
            else
                return this.Fail(ValTuple.Errors);
        }

        #endregion
    }
}